/*
 * @FileName: analog.h
 * @Author: GreyQiu qiushaogui@aikosolar.com
 * @Date: 2023-08-09 14:12:04
 * @LastEditors: GreyQiu qiushaogui@aikosolar.com
 * @LastEditTime: 2023-09-18 18:50:00
 * @Description: 
 * 
 * Copyright (c) 2023, All Rights Reserved. 
 */

#ifndef __ANALOG__H__
#define __ANALOG__H__

#include <stdint.h>
#include "easy_filter.h"
#include "app_calibration.h"

/**
 * @description: 模拟信号单元
 */
typedef struct {
    uint32_t id;
    uint32_t raw;           // ADC原始数据
    float k;                // 用于ADC值转换为真实值的比例K
    float b;                // 用于ADC值转换为真实值的偏置B
    float Fc;               // 滤波器截止频率
    float Ts;               // 采样周期
    easy_lpf_f32_t lpf;     // 一阶低通滤波器
    float real;             // 滤波前真实值
    float real_flt;         // 滤波后的真实值
    float k1;                // 校准K
    float b1;                // 校准B
} analog_t;

/**
 * @description: 模拟信号值计算
 * @param obj    模拟信号对象地址
 * @return 无
 */
static inline void analog_convert(analog_t * obj, uint32_t adcRaw)
{
    obj->raw = adcRaw;
    obj->real = (obj->k * adcRaw + obj->b) * obj->k1 + obj->b1;
}

/**
 * @description: 模拟信号滤波
 * @param obj    模拟信号对象地址
 * @return 无
 */
static inline void analog_filter(analog_t * obj)
{
    obj->real_flt = easy_lpf_f32(&(obj->lpf), obj->real); // 一阶低通滤波
}

#endif
